<?php


namespace app\api\controller;


class Jingyouquan extends ApiBase
{
    public function getList()
    {
        $page = input("page", 1);
        $pageSize = input("page_size", 10);
        $keyword = input("keyword", "");
        $res = \app\common\model\Jingyouquan::getList($page, $pageSize, $keyword);
        if ($res['code'] != 0) {
            $this->error($res['msg']);
        }
        $this->success($res['data']);
    }

    public function add()
    {
        $this->checkUser();
        $user = $this->getUser();
        $content = input("content", "");
        if (!$content) {
            $this->error("请填写京口令内容");
        }
        $where=[];
        $where[]=['content','=',$content];
        $model=\app\common\model\Jingyouquan::where($where)->find();
        if($model){
            $this->error("该京口令已经存在，请不要重复发布");
        }

        $jingyouquan = new \app\common\model\Jingyouquan([
            "user_id" => $user->id,
            "content" => $content,
            "copy_counts" => 0
        ]);
        $jingyouquan->create_time = $jingyouquan->update_time = getNow();
        $res = $jingyouquan->save();
        if ($res === false) {
            $this->error("数据库读写错误");
        }
        $this->success($jingyouquan);
    }

    public function addCopyCounts()
    {
        $id = input("id", 0);
        if (!$id) {
            $this->error("请传入id");
        }
        $model = \app\common\model\Jingyouquan::find($id);
        if (!$model) {
            $this->error("记录未找到");
        }
        $model->copy_counts++;

        $model->save();
        $this->success($model);

    }

}
